<?php
namespace app\app\controller;

use think\Controller;
class Report extends Controller {
    /*
     * app--报表--
     * 获取所有订单的基本信息
     * @return 
     */
    public function index() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $username = $data['post_username'];
            // 通过用户名 获取 用户相关信息
            $ret = model('AppBisOrder')->where(
                        ['username' => ['=', trim($username)],]
                    )
                    ->order('update_time', 'desc')
                    ->select();
            if($ret) {
                $wxnum = 0;
                $wxmoney = 0;
                $zfbnum = 0;
                $zfbmoney = 0;
                for($i = 0; $i<count($ret); $i++){
                    $list[$i] = array(
                        'orderId' => $ret[$i] ->order_id,
                        'image' => $ret[$i] ->image,
                        'title' => $ret[$i] ->title,
                        'titleColor' => $ret[$i] ->title_color,
                        'money' => $ret[$i] ->money,
                        'date' => $ret[$i] ->update_time,                    
                    );
                    if($ret[$i] ->image == 1){
                        $wxnum ++;
                        $wxmoney += $ret[$i] ->money;
                    }
                    if($ret[$i] ->image == 2){
                        $zfbnum ++;
                        $zfbmoney += $ret[$i] ->money;
                    }
                }
                
                $newWxmoney  = $this->comparemoney($wxmoney);
                $newZfbmoney = $this->comparemoney($zfbmoney);
                $allmoney    = $wxmoney + $zfbmoney;
                $newAllmoney = $this->comparemoney($allmoney);

                $arr = array(
                    'wxnum'    => $wxnum,
                    'wxmoney'  => $newWxmoney,
                    'zfbnum'   => $zfbnum,
                    'zfbmoney' => $newZfbmoney,
                    'allmoney' => $newAllmoney,
                    'list'     => $list,
                );
                return json_encode($arr, 320);
            } else {
                return json_encode(false);
            }
        }
    }  
    
    public function comparemoney($money) {
        if($money > 0) {
            $newMoney = '+' . number_format($money,2);
        } else {
            $newMoney = number_format($money,2);
        }
        return $newMoney;
    }
    
    /*
     * 获取单个订单的详细信息
     * @return 
     */
    public function orderinfo() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $orderId = $data['order_id'];
            // 通过用户名 获取 用户相关信息
            $ret = model('AppBisOrder')->get(['order_id'=>$orderId]);
            if($ret){
                $arrdata=array(
                    'orderId' => $orderId,
                    'money' => $ret -> money,
                    'date' => $ret -> update_time,
                    'remark' => $ret -> remark,
                    'image' => $ret -> image
                );
            }
            return json_encode($arrdata, 320);
        }
    } 
}